package main.java.org.dss.lm.admin.controller.admin;

import java.util.ArrayList;
import java.util.List;

import main.java.org.dss.lm.admin.bean.UserBean;
import main.java.org.dss.lm.admin.common.ITSConstants;
import main.java.org.dss.lm.admin.dao.admin.IUserService;
import main.java.org.dss.lm.admin.dao.admin.UserService;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class EditOrDeleteUserDetails extends ActionSupport  implements ModelDriven<UserBean>, ITSConstants
{
    private UserBean userBean = new UserBean();
    private IUserService userService = new UserService();
    
    List<UserBean> usersInDatabase;

    /**
     * This method is an overridden method of ModelDriven class which returns login entity object,
     * populated with the login form.
     *
     * @return the AccountEntity object.
     */
    
    @Override
    public UserBean getModel()
    {
        return userBean;
    }
    
    public String allUser()
    {
    	usersInDatabase = new ArrayList<UserBean>();
    	try {
    		List<UserBean> userList = userService.getUsersInDatabase();
    		
    		usersInDatabase = userService.getGroupsForUser(userList);
    		
        } catch (Exception exp) {            
        }

        return ITSConstants.SUCCESS;
    }
        
    public String editOrDeleteUserDetails()
    {    	
    	if(ITSConstants.EDIT.equals(getModel().getEditOrDelete())){
    		return ITSConstants.EDIT;   		
    	}
    	if(ITSConstants.DELETE.equals(getModel().getEditOrDelete())){
    		return ITSConstants.DELETE;
    	}    	
    	return ITSConstants.EDIT;
    }
    
    public String deleteUser()
    {
    	try {
    		if(getModel().getDeleteStatus().equals("Cancel")) return ITSConstants.SUCCESS;
    		return userService.deleteUser(getModel());
    	} catch (Exception exp) {            
        }
        return ITSConstants.INPUT;
    }
       
    
//---------------------------------------------------------------------------------
    
    public List<UserBean> getUsersInDatabase() {
		return usersInDatabase;
	}

	public void setUsersInDatabase(List<UserBean> usersInDatabase) {
		this.usersInDatabase = usersInDatabase;
	}

}// End Class LoginAction
